package com.example.husbandry_online;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.viewpager2.widget.ViewPager2;

import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.example.husbandry_online.adapter.HomePageFragmentAdapter;
import com.example.husbandry_online.view.AddFragment;
import com.example.husbandry_online.view.HomeFragment;
import com.example.husbandry_online.view.HusbandryFragment;
import com.example.husbandry_online.view.LivebroadcastFragment;
import com.example.husbandry_online.view.LoginFragment;
import com.example.husbandry_online.view.MessageFragment;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.android.material.bottomnavigation.BottomNavigationView.OnNavigationItemSelectedListener;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;
import com.ittianyu.bottomnavigationviewex.BottomNavigationViewEx;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private BottomNavigationViewEx navigation;
    private List<Fragment> fragments;
    MessageFragment messageFragment;
    AddFragment addFragment;
    HomeFragment homeFragment;
    //用于记录上个选择的Fragment
    private int lastFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
        loadFragment();
        initNavigation();
    }

    /**
     * 相关初始化
     */
    private void init() {
        fragments = new ArrayList<>();
        homeFragment = new HomeFragment();
        addFragment = new AddFragment();
        messageFragment = new MessageFragment();
        fragments.add(homeFragment);
        fragments.add(addFragment);
        fragments.add(messageFragment);
        lastFragment = 0;

        //顶部状态栏颜色设置
//        Window window = getWindow();
//        //After LOLLIPOP not translucent status bar
//        window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
//        //Then call setStatusBarColor.
//        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
//        window.setStatusBarColor(getResources().getColor(R.color.white));
//        //顶部状态栏透明
//        if(Build.VERSION.SDK_INT >= 21) {
//            Window window = getWindow();
//            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
//                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
//            window.setStatusBarColor(Color.TRANSPARENT);
//        }
    }

    /**
     * 底部Navigation的相关初始化
     */
    private void initNavigation() {
        navigation = (BottomNavigationViewEx) findViewById(R.id.bnve);
        navigation.setItemIconTintList(null);

        navigation.setOnNavigationItemSelectedListener(new OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch (item.getItemId())
                {
                    case R.id.action_home:
                    {
                        if(lastFragment!=0)
                        {
                            switchFragment(lastFragment,0);
                            lastFragment=0;
                        }
                        return true;
                    }
                    case R.id.action_add:
                    {
                        if(lastFragment!=1)
                        {
                            switchFragment(lastFragment,1);
                            lastFragment=1;
                        }
                        return true;
                    }
                    case R.id.action_message:
                    {
                        if(lastFragment!=2)
                        {
                            switchFragment(lastFragment,2);
                            lastFragment=2;
                        }
                        return true;
                    }
                }
                return false;
            }
        });
    }

    /**
     * 切换Fragment
     * @param lastfragment
     * @param index
     */
    private void switchFragment(int lastfragment,int index)
    {
        FragmentTransaction transaction =getSupportFragmentManager().beginTransaction();
        transaction.hide(fragments.get(lastfragment));//隐藏上个Fragment
        if(!fragments.get(index).isAdded())
        {
            transaction.add(R.id.container_frame, fragments.get(index));
        }
        transaction.show(fragments.get(index)).commitAllowingStateLoss();

    }

    /**
     * 初始化第一个界面
     */
    private void loadFragment() {
//        getSupportFragmentManager().beginTransaction()
//                .add(R.id.container_frame,homeFragment)
//                .commit();
        //创建默认Fragment
        createFragment(homeFragment);


    }

    //创建Fragment
    public void createFragment(Fragment fragment){
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.add(R.id.container_frame,fragment);
        fragmentTransaction.commit();
    }
    //更改Fragment
    public void changeFragment(Fragment fragment) {
        //得到管理类
        FragmentManager fragmentManager = getSupportFragmentManager();
        //开始事务
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        //事务创建
        fragmentTransaction.replace(R.id.container_frame,fragment);

        fragmentTransaction.addToBackStack(null);
        //事务提交
        fragmentTransaction.commit();
    }

}